package com.lzj.myapplication.base;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lzj.myapplication.R;
import com.lzj.myapplication.util.NetUtil;


/**
 * Created by lizj on 2017/10/11.
 */

public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener{
    private LinearLayout ll_base;
    private View activityLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置竖屏
        setActivityState(this);
        setContentView(R.layout.activity_base);
        this.ll_base = ((LinearLayout)findViewById(R.id.ll_base));
        activityLayout = getLayoutInflater().inflate(getLayoutId(), null);
        this.ll_base.addView(activityLayout);
        initData();
        setListener();
        requestServer();
    }

    /**
     * 设置contentview
     * @return
     */
    public abstract int getLayoutId();

    /**
     *初始化控件和数据
     */
    protected abstract void initData() ;

    /**
     *网络请求
     */
    protected abstract void requestServer() ;

    /**
     *添加监听
     */
    protected abstract void setListener() ;



    // Toast
    protected void showToast(CharSequence msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    // Toast
    protected void showTtoast(int resId) {
        Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
    }

    // startActivity
    protected void goActivity(Class<?> cls) {
        goActivity(cls, null);
    }

    // startActivity
    protected void goActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    // startActivityForResult
    protected void goActivityForResult(Class<?> cls, int requestCode) {
        goActivityForResult(cls, null, requestCode);
    }

    // startActivityForResult
    protected void goActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    // getIntent
    protected Bundle getIntentExtra() {
        Intent intent = getIntent();
        Bundle bundle = null;
        if (null != intent)
            bundle = intent.getExtras();
        return bundle;
    }

    // findViewById
    public <T extends View> T findView(int resId) {
        return (T) findViewById(resId);
    }

    /**
     * 设置屏幕只能竖屏
     * @param activity
     *                  activity
     */
    public void setActivityState(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /**
     * 检测网络是否可用
     */
    public boolean isNetConnect(){
        return NetUtil.isNetworkAvailable(this); // NetUtil 是我自己封装的类
    }

}
